package com.shop.service.impl;

import com.shop.common.Message;
import com.shop.entity.Member;
import com.shop.entity.Order;
import com.shop.mapper.MemberMapper;
import com.shop.mapper.OrderMapper;
import com.shop.service.MemberService;
import com.shop.Exception.DuplicateDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 会员服务实现类
 * 
 * 提供会员相关的业务逻辑处理
 * 
 * @author Shop Management System
 * @version 1.0
 * @since 2024-01-01
 */
@Service
@Transactional
public class MemberServiceimpl implements MemberService {
    
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public List<Member> findAllMembers() {
        return memberMapper.findAll();
    }

    @Override
    public Member findMemberById(Long id) {
        return memberMapper.findById(id);
    }

    @Override
    public void addMember(Member member) {
        // 检查手机号是否已存在
        if (member.getPhone() != null && !member.getPhone().isEmpty()) {
            Member existingMember = memberMapper.findByPhoneNumber(member.getPhone());
            if (existingMember != null) {
                throw new DuplicateDataException( "手机号"+ member.getPhone() + Message.PHONE_IS_BE_USED);
            }
        }
        
        // 检查会员卡号是否已存在
        if (member.getCardNumber() != null && !member.getCardNumber().isEmpty()) {
            Member existingMember = memberMapper.findByCardNumber(member.getCardNumber());
            if (existingMember != null) {
                throw new DuplicateDataException("会员卡号 " + member.getCardNumber() + Message.MEMBER_IS_USED);
            }
        }
        
        if (member.getCardNumber() == null || member.getCardNumber().isEmpty()) {
            member.setCardNumber("VIP" + UUID.randomUUID().toString().substring(0, 8).toUpperCase());
        }
        member.setCreatedAt(LocalDateTime.now());
        member.setUpdatedAt(LocalDateTime.now());
        if (member.getPoints() == null) {
            member.setPoints(0);
        }
        memberMapper.insert(member);
    }

    @Override
    public void updateMember(Member member) {
        member.setUpdatedAt(LocalDateTime.now());
        memberMapper.update(member);
    }

    @Override
    public void deleteMember(Long id) {
        //先查询有没有正在进行的订单
        //有则进行
        Member member = memberMapper.findById(id);
        member.setIsDeleted(1);
        member.setUpdatedAt(LocalDateTime.now());
        Order order = orderMapper.findUnfinishedOrdersByMemberId(id);
        if(order != null) {
            throw new RuntimeException(Message.ORDER_IS_NOT_COMPLETED);
        }
        memberMapper.update(member);
    }

    @Override
    public Member findMemberByPhoneNumber(String phoneNumber) {
        return memberMapper.findByPhoneNumber(phoneNumber);
    }

    @Override
    public Member findMemberByCardNumber(String cardNumber) {
        Member member = memberMapper.findByCardNumber(cardNumber);
        if (member != null) {
            member.setUpdatedAt(LocalDateTime.now());
            memberMapper.update(member);
        }
        return member;
    }

    @Override
    public void updateMemberConsumption(Long memberId, Double amount) {
        Member member = findMemberById(memberId);
        if (member != null) {
            // Business logic for consumption and points can be added here
            member.setUpdatedAt(LocalDateTime.now());
            memberMapper.update(member);
        }
    }
} 
 